//
//  Factory.h
//  zhaitu
//
//  Created by ellzu on 13-8-1.
//  Copyright (c) 2013年 ellzu. All rights reserved.
//

#ifndef __zhaitu__Factory__
#define __zhaitu__Factory__

#include <iostream>
#include <map>
#include "ProductorInterface.h"


namespace EZCRefectFactory {
    

#define RegistClassWithClassName(T) EZCRefectFactory::Factory::Instance()->regist(#T ,(EZCRefectFactory::ProductorInterface*)new EZCRefectFactory::Productor<T>());
#define RegistClassWithIdName(T) EZCRefectFactory::Factory::Instance()->regist(typeid(T).name() ,(EZCRefectFactory::ProductorInterface*)new EZCRefectFactory::Productor<T>());
    
#define RegistClass(T) EZCRefectFactory::Factory::Instance()->regist(#T ,(EZCRefectFactory::ProductorInterface*)new EZCRefectFactory::Productor<T>());\
                        EZCRefectFactory::Factory::Instance()->regist(typeid(T).name() ,(EZCRefectFactory::ProductorInterface*)new EZCRefectFactory::Productor<T>());
    
#define NewObject(name) EZCRefectFactory::Factory::Instance()->newObject(name);
    
    class Factory {
    public:
        static Factory* Instance()
        {
            static Factory *instance = NULL;
            if (!instance) {
                instance = new Factory;
            }
            return instance;
        }
        void regist(const std::string &name , EZCRefectFactory::ProductorInterface *pro)
        {
            map[name]=pro;
        }
        void* newObject(const std::string name)
        {
            return map[name]->create();
        }
        std::map<std::string, ProductorInterface* > map;
    };

}
#endif /* defined(__zhaitu__Factory__) */
